package com.aqie.meet.ui.chat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.aqie.libcommon.app.BaseActivity;
import com.aqie.libcommon.app.BaseFragment;
import com.aqie.libfactory.model.db.Session;
import com.aqie.meet.R;

public class MessageActivity extends BaseActivity {
    // 接收者Id，可以是群，也可以是人的Id
    public static final String KEY_RECEIVER_ID = "KEY_RECEIVER_ID";
    // 是否是群
    private static final String KEY_RECEIVER_IS_GROUP = "KEY_RECEIVER_IS_GROUP";
    public static final String USERNAME = "USERNAME";
    public static final String KEY_CAN_TALK = "KEY_CAN_TALK";

    // 取上面参数值
    private String mReceiverId;
    private String mReceiverUsername;
    private boolean mIsGroup = false;
    private boolean canTalk = true;

    /**
     * 用户点对点聊天
     * @param mContext
     * @param receiverId
     */
    public static void show(Context mContext, String receiverId) {
        if(mContext == null || TextUtils.isEmpty(receiverId)) {
            return;
        }
        Intent intent = new Intent(mContext, MessageActivity.class);
        intent.putExtra(KEY_RECEIVER_ID, receiverId);
        intent.putExtra(KEY_RECEIVER_IS_GROUP, false);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    public static void show(Context mContext, String receiverId, String userName) {
        if(mContext == null || TextUtils.isEmpty(receiverId)) {
            return;
        }
        Intent intent = new Intent(mContext, MessageActivity.class);
        intent.putExtra(KEY_RECEIVER_ID, receiverId);
        intent.putExtra(USERNAME, userName);
        intent.putExtra(KEY_RECEIVER_IS_GROUP, false);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    public static void show(Context mContext, String receiverId, String userName, Boolean canTalk) {
        if(mContext == null || TextUtils.isEmpty(receiverId)) {
            return;
        }
        Intent intent = new Intent(mContext, MessageActivity.class);
        intent.putExtra(KEY_RECEIVER_ID, receiverId);
        intent.putExtra(USERNAME, userName);
        intent.putExtra(KEY_RECEIVER_IS_GROUP, false);
        intent.putExtra(KEY_CAN_TALK, canTalk);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    /**
     * 通过Session发起聊天
     *
     * @param context 上下文
     * @param session Session
     */
    public static void show(Context context, Session session) {
        if (session == null || context == null || TextUtils.isEmpty(session.getId()))
            return;
        Intent intent = new Intent(context, MessageActivity.class);
        intent.putExtra(KEY_RECEIVER_ID, session.getId());
        intent.putExtra(USERNAME, session.getTitle());
        intent.putExtra(KEY_RECEIVER_IS_GROUP, false);
        intent.putExtra(KEY_CAN_TALK, true);
        context.startActivity(intent);
    }

    public static void show(Context context, Session session, Boolean canTalk) {
        if (session == null || context == null || TextUtils.isEmpty(session.getId()))
            return;
        Intent intent = new Intent(context, MessageActivity.class);
        intent.putExtra(KEY_RECEIVER_ID, session.getId());
        intent.putExtra(USERNAME, session.getTitle());
        intent.putExtra(KEY_RECEIVER_IS_GROUP, false);
        intent.putExtra(KEY_CAN_TALK, canTalk);
        context.startActivity(intent);
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        mReceiverId = bundle.getString(KEY_RECEIVER_ID);
        mReceiverUsername = bundle.getString(USERNAME);
        mIsGroup = bundle.getBoolean(KEY_RECEIVER_IS_GROUP);
        canTalk = bundle.getBoolean(KEY_CAN_TALK);
        return !TextUtils.isEmpty(mReceiverId);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle("Message");
        BaseFragment fragment;
        if (mIsGroup)
            fragment = new ChatUserFragment();
        else
            fragment = new ChatUserFragment();

        // 从Activity传递参数到Fragment中去
        Bundle bundle = new Bundle();
        bundle.putString(KEY_RECEIVER_ID, mReceiverId);
        bundle.putString(USERNAME, mReceiverUsername);
        bundle.putBoolean(KEY_CAN_TALK, canTalk);
        fragment.setArguments(bundle);

        // 在activity_message.xml 中的 R.id.lay_container
        getSupportFragmentManager().beginTransaction()
                .add(R.id.lay_container, fragment)
                .commit();

    }


}